import Command from "../enum/Command.js"
import BufferTool from "../tool/BufferTool.js"


export default class NegotiateAgree {

  static command = Command.NEGOTIATE
  static structure = {
    request : [
      ['StructureSize', 2, 36],  // 客户端设置固定值为36，没有包括Dialects的长度
      ['DialectCount', 2, 2], // 在Dialects中给出的协议的个数
      ['SecurityMode', 2, 1], // SMB signing是否启用。 1表示 安全签名在客户端可用，服务端要忽略此值。 2表示客户端要求安全签名。
      ['Reserved', 2, 0], // 固定值0 Windows 8.1 operating system and later and Windows Server 2012 R2 operating system and later fail the request with STATUS_NOT_SUPPORTED if the Reserved field is set to a nonzero value.
      ['Capabilities', 4, 0],//  If the client implements the SMB 3.x dialect family, the Capabilities field MUST be constructed using the following values. Otherwise, this field MUST be set to 0.
      ['ClientGuid', 16, 0], //It MUST be a GUID (as specified in [MS-DTYP] section 2.3.4.2) generated by the client.
     
      ['ClientStartTime', 8, 0], //(NegotiateContextOffset,NegotiateContextCount,Reserved2)/ClientStartTime (8 bytes): This field is interpreted in different ways depending on the SMB2 Dialects field.Use as ClientStartTime This field MUST NOT be used and MUST be reserved. The client MUST set this to 0, and the server MUST ignore it on receipt.
   
      /**
      An array of one or more 16-bit integers specifying the supported dialect revision
      numbers. The array MUST contain at least one of the following values.
      Value Meaning
      用2个字节表示一个协议
      0x0202 SMB 2.0.2 dialect revision number.
      0x0210 SMB 2.1 dialect revision number.<9>
      0x0300 SMB 3.0 dialect revision number. <10>
      0x0302 SMB 3.0.2 dialect revision number.<11>
      0x0311 SMB 3.1.1 dialect revision number.<12>
      */
      ['Dialects', 4, BufferTool.dataListToBuffer([[0x0202, 2], [0x0210, 2]]) ], 
    ],
    response : [
      ['StructureSize', 2],  //固定值65，不管Buffer的长度。

      /**
        SMB2_NEGOTIATE_SIGNING_ENABLED
        0x0001
        When set, indicates that security signatures are enabled on the
        server.
        SMB2_NEGOTIATE_SIGNING_REQUIRED
        0x0002
        When set,
       */
      ['SecurityMode', 2],


      ['DialectRevision', 2], //确定要使用的协议版本
      ['Reserved', 2], // 0x0311版本有含义，否则是保留字段。
      ['ServerGuid', 16],
      ['Capabilities', 4],

      /**
      The maximum size, in bytes, of the buffer that can be used for
QUERY_INFO, QUERY_DIRECTORY, SET_INFO and CHANGE_NOTIFY operations. This field is
applicable only for buffers sent by the client in SET_INFO requests, or returned from the server in
QUERY_INFO, QUERY_DIRECTORY, and CHANGE_NOTIFY responses.<26>
       */
      ['MaxTransactSize', 4],


      ['MaxReadSize', 4],
      ['MaxWriteSize', 4],
      ['SystemTime', 8],
      ['ServerStartTime', 8],
      ['SecurityBufferOffset', 2], //The offset, in bytes, from the beginning of the SMB2 header to the security buffer.
      ['SecurityBufferLength', 2],
      ['Reserved2', 4],
      /**
        包含响应的安全缓冲区的可变长度缓冲区，如
        由SecurityBufferOffset和SecurityBufferLength指定。缓冲区应包含
        第3.3.5.4节规定的GSS协议产生的令牌。如果SecurityBufferLength
        为0，此字段为空，然后客户端启动身份验证，身份验证协议为
        客户端的选择将被用来代替服务器发起的SPNEGO身份验证，如
        [MS-AUTHSOD]第2.1.2.2节。
       */
      ['Buffer', 'SecurityBufferLength'],
    ]
  }


  constructor(){


  }

  static onRequest(){

  }

  static onResponse(connectObj, message){
    console.log("协议版本:", message.bodyStrucFormat.DialectRevision)
    
  }

}